﻿using Super_Management_Mayhem.Content.Spritesheet;
using Super_Management_Mayhem.Control;
using Super_Management_Mayhem.Screens.Menu;
using Super_Management_Mayhem.Screens.Menu.SelectGameMode;
using Super_Management_Mayhem.Screens.Menu.SelectPlayers;
using Super_Management_Mayhem.Screens.Playing.GameMode;
using Super_Management_Mayhem.Screens.Playing.UI;
using Transformable_Engine_v2.Engine;
using Transformable_Engine_v2.Engine.GameObjects.Containers;
using Transformable_Engine_v2.Engine.Input;
using Transformable_Engine_v2.Engine.Transformable_Math.Geometry;

namespace Super_Management_Mayhem.GameRoot
{
    public class Config : Container
    {
        private BaseMenuScreen<Menu> _currentMenu;

        private InputKeys _input;
        private Menu _oldMenu;
        public GameMode.GameModes GameMode { get; private set; }

        public SelectPlayers.PlayerCount Players { get; private set; }

        public enum Menu
        {
            None,
            SelectPlayerCount,
            SelectGameMode,
            SelectTeamCount,
            Start,
        }

        public override void LoadContent()
        {
            MenuSheet.LoadContent();
            BorderSheet.LoadContent();

            AddChildren(new OuterBorder());

            _input = InputKeys.FromFile("Content/Config/Input/Player1.input");
            KeyboardHandler.OnKeyboardPress += OnKeyPress;

            ChangeMenu(-1, Menu.SelectPlayerCount);

            base.LoadContent();
        }

        private void ChangeMenu(int id, Menu newMenu)
        {
            if (_currentMenu != null)
            {
                _currentMenu.OnChangeScreen -= ChangeMenu;
                _currentMenu.Die();

                switch (_oldMenu)
                {
                    case Menu.SelectPlayerCount:
                        Players = (_currentMenu as SelectPlayers).Players;
                        break;
                    case Menu.SelectGameMode:
                        GameMode = (_currentMenu as SelectGameModeMenu).Mode;
                        break;
                }
            }

            switch (newMenu)
            {
                case Menu.SelectPlayerCount:
                    _currentMenu = new SelectPlayers { PositionGridBound = new TE_Vector2(2, 2) };
                    break;
                case Menu.SelectGameMode:
                    _currentMenu = new SelectGameModeMenu(Game.Options.ScreenSize.Offset(-4, -4)) { PositionGridBound = new TE_Vector2(2, 2) };
                    break;
                case Menu.Start:
                    Game.Close();
                    break;
            }

            _oldMenu = newMenu;
            _currentMenu.OnChangeScreen += ChangeMenu;

            AddChildren(_currentMenu);
        }

        private void OnKeyPress(KeyboardInput input)
        {
            if (input.KeyCode == _input.UpKey)
            {
                _currentMenu.UpKey(-1);
            }
            else if (input.KeyCode == _input.LeftKey)
            {
                _currentMenu.LeftKey(-1);
            }
            else if (input.KeyCode == _input.RightKey)
            {
                _currentMenu.RightKey(-1);
            }
            else if (input.KeyCode == _input.DownKey)
            {
                _currentMenu.DownKey(-1);
            }
            else if (input.KeyCode == _input.AttackKey)
            {
                _currentMenu.AcceptKey(-1);
            }
            else if (input.KeyCode == _input.ActionKey)
            {
                _currentMenu.CancelKey(-1);
            }
        }
    }
}
